package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.service.EmployeeService;
import com.itheima.reggie.entity.Employee;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录方法
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request,@RequestBody Employee employee){
        //1.对密码进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.根据username查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<Employee>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);


        //3.查看用户是否存在
        if (emp == null ){
            return R.error("登陆失败");
        }

        //4.校验密码是否正确
        if (!password.equals(emp.getPassword())){
            return R.error("登陆失败");
        }

        //5.校验用户状态
        if (emp.getStatus() == 0){
            return R.error("账号已禁用");
        }

        //6.登陆成功，将用户id写入session,并返回登陆成功的结果
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }


    /**
     * 退出功能
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //1.把session中当前用户的id删掉
        request.getSession().removeAttribute("employee");

        //返回结果
        return R.success("退出成功");

    }

    /**
     * 添加员工功能
     * @param request
     * @param employee
     * @return
     */

    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        //log.info("新增员工信息:{}",employee.toString());
        //设置密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //设置时间
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());

        //long id = (long)request.getSession().getAttribute("employee");

        //设置用户
        //employee.setCreateUser(id);
        //employee.setUpdateUser(id);

        //保存数据到数据库
        employeeService.save(employee);

        return R.success("新增员工成功");
    }

    /**
     * 分页查询功能
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //log.info("page:{},pageSize{},name:{}",page,pageSize,name);

        //构建分页构造器
        Page<Employee> pageInfo = new Page<>(page,pageSize);

        //构建条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        queryWrapper.orderByAsc(Employee::getUpdateTime);

        //执行查询
        employeeService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }


    /**
     * 更新操作
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        //log.info("哈哈哈"+employee.toString());

        Long empId = (Long) request.getSession().getAttribute("employee");

        //更新修改的时间和修改的人
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);

        //执行SQL
        employeeService.updateById(employee);

        return R.success("员工信息修改成功");
    }


    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        //log.info("根据id查询员工信息:{}",id);

        Employee employee = employeeService.getById(id);
        if (employee != null){
            return R.success(employee);
        }
        return R.error("未查到相应员工信息");

    }

}
